﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Logic.Personal;
using Job.Web.Framework;
using Job.Entity;
using Job.Logic.BaseContent;
using System.Collections.Generic;
using Job.Logic.Common;

namespace Job.Web.Person
{
    public partial class SendResume : BasePage
    {
        private int personAccountId
        {
            get
            {
                //后台管理员能通过后台为企业增加职位，所以在此处需要兼职企业ID从url或从Session中获取
                MySession session = new MySession();
                SessionPersonal sPerson = session.GetPersonalFromSession();
                if (null == sPerson)
                    return 0;
                else
                    return sPerson.PersonAccountid;
            }
        }
        private string JobNo
        {
            get { return myR["job"].StringValue; }
        }

        PersonalLogic pl = new PersonalLogic();

        protected void Page_Load(object sender, EventArgs e)
        {
            this.msgTd.Visible = false;
            if (!IsPostBack)
            {
                if (JobNo == null || JobNo.Length < 0)
                {
                    ShowMessage("请选择猎头职位后才投递简历！");
                    return;
                }

                JobHunter job = new HunterJobLogic().GetDetail(JobNo);
                if (job == null)
                {
                    ShowMessage("选择的职位已不存在！");
                    return;
                }

                this.msgTd.InnerText = "职位：(" + job.JobNo + ")" + job.Name
                    + "，请选择一个简历发送：";
                this.msgTd.Visible = true;

                this.LoadData();
            }
        }

        private void LoadData()
        {

            IList<ResumeList> list = pl.GetResumeList(personAccountId);
            dvResume.DataSource = list;
            dvResume.DataBind();
        }

        protected void dvResume_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lbApplyStyle = (Label)e.Row.FindControl("lbApplyStyle");
                if (null != lbApplyStyle)
                {
                    Label lbApplyName = (Label)e.Row.FindControl("lblApplyName");
                    if (null != lbApplyName) lbApplyName.Text = MemoryDictionary.GetCommonDictName(lbApplyStyle.Text);
                }

                Label lblID = (Label)e.Row.FindControl("lblid");
                HyperLink hlModify = (HyperLink)e.Row.FindControl("linkModify");
                if (hlModify != null)
                {
                    string url = WebCommon.GetApplicationPath(Request)
                        + "Person/PersonalResume.aspx?subjectid=1&categoryid=1&resumeid=" + lblID.Text;
                    hlModify.NavigateUrl = url;
                }
                HyperLink hlResume = (HyperLink)e.Row.FindControl("linkResume");
                if (hlResume != null)
                {
                    string url = WebCommon.GetApplicationPath(Request)
                        + "Common/ResumeBrowse.aspx?categoryid=1&resumeid=" + lblID.Text;
                    hlResume.NavigateUrl = url;
                }
            }
        }

        protected void dvResume_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName.ToLower().Equals("send") )
            {
                try
                {
                    JobHunter job = new HunterJobLogic().GetDetail(JobNo);
                    if (job != null)
                    {
                        ApplyJobLogic logic = new ApplyJobLogic();

                        JobSendResume resume = new JobSendResume();
                        resume.PersonAccountId = personAccountId;
                        resume.JobId = job.JobId;
                        resume.PersonInfoId = Convert.ToInt32(e.CommandArgument.ToString());

                        logic.SendResume(resume);
                    }
                    this.msgTd.InnerText = 
                            this.msgTd.InnerText.Replace("请选择一个简历发送：", "已将简历发送到该猎头职位！");
                    this.msgTd.Visible = true;
                }
                catch (Exception ex)
                {
                    this.msgTd.InnerHtml = "<font color='#990000'> 内部异常，发送简历失败，请稍后再试！</font>";
                    this.msgTd.Visible = true;
                }
            }

        }


        private void ShowMessage(String message)
        {
            Response.Write("<script>\n");
            Response.Write(" alert('" + message + "');\n");
            Response.Write(" location.href='" + WebCommon.GetApplicationPath(Request) + "Index.aspx';\n");
            Response.Write("</script>");
            //this.msgTd.InnerHtml = "<font color='#990000'>" + message + "</font>";
            //this.msgTd.Visible = true;
        }
    }
}
